package com.shzu.jwc.ty.controller;

import java.util.List;

import com.shzu.jwc.ty.common.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shzu.jwc.ty.bean.SystemSelectcourse;
import com.shzu.jwc.ty.common.Msg;
import com.shzu.jwc.ty.common.Semester;
import com.shzu.jwc.ty.service.SelectCourseservice;
import com.shzu.jwc.ty.service.SystemYearservice;

@Controller
@RequestMapping("/jwc/selectcourse")
public class SelectCoursecontroller {

	@Autowired
	SelectCourseservice selectCourseservice;
	
	@Autowired
	SystemYearservice systemYearservice;
	
	@RequestMapping("/selectcourse.action")
	public String showselectcourse(@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		Page page=new Page(selectCourseservice.countcourse(),pn);
		page.setPageSize(20);
		Integer systemYearId=systemYearservice.getoneyearby(Semester.getnow());
		List<SystemSelectcourse> data=selectCourseservice.getcourse(systemYearId,page);
		model.addAttribute("data", data);
		model.addAttribute("page",page);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "jwc/selectcourse/selectcourse";
	}
	
	@ResponseBody
	@RequestMapping(value="/upload.action",method=RequestMethod.POST)
	public Msg upload(@RequestParam("file") MultipartFile file) {
		if(file.isEmpty()) {
			return Msg.error("文件不能为空");
		}
		String name=file.getOriginalFilename();
		if(!name.endsWith(".xlsx")) {
			return Msg.error("文件类型必须为xlsx");
		}
		try {
			selectCourseservice.batchImport(name, file);
			return Msg.success("操作成功");
		}catch(Exception ex) {
			return Msg.error("操作失败,"+ex.getMessage());
		}
	}
	
	@RequestMapping("/searchselectcourse.action")
	public String searchselectcourse(@RequestParam("userStudentInfoNum")String userStudentInfoNum,@RequestParam(value="pn",defaultValue="1")Integer pn,Model model) {
		List<SystemSelectcourse> data=selectCourseservice.search(userStudentInfoNum);
		model.addAttribute("data", data);
		model.addAttribute("systemyear", systemYearservice.getSystemYear());
		model.addAttribute("nowyear",Semester.getnow());
		return "jwc/selectcourse/selectcourse";
	}
}
